package Utils;

import Bean.Business;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;

public class JdbcUtils {
	private static DruidDataSource dataSource;
	static {
		Properties properties=new Properties();
		InputStream stream = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
		try {
			//将xxx.properties属性集文件中的kv对载入到属性集对象中  属性集本身就是一个map结构
			properties.load(stream);
		} catch (IOException e) {
			e.printStackTrace();
		}

		dataSource=new DruidDataSource();
		dataSource.setDriverClassName(properties.getProperty("driver"));
		dataSource.setUrl(properties.getProperty("url"));
		dataSource.setUsername(properties.getProperty("username"));
		dataSource.setPassword(properties.getProperty("password"));
	}

	public static DruidDataSource getDataSource() {
		return dataSource;
	}
	public static Connection getConn( ) throws SQLException {
		return dataSource.getConnection();
	}
	public static QueryRunner getQueryRunner() {
		return new QueryRunner(dataSource);
	}

	public static void main(String[] args) throws SQLException {
		QueryRunner queryRunner=getQueryRunner();
		String sql = "select *from t_business";
		List<Business> list=queryRunner.query(sql,new BeanListHandler<Business>(Business.class));
		list.forEach(System.out::println);
	}
}
